package servlet;

import dao.UsuarioDao;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Usuario;

/**
 *
 * @author Jeisse
 */
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     processRequest(req, resp);   
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        processRequest(req, resp);
    }
   
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        response.setContentType("text/html;charset=UTF-8");
                
        String email = request.getParameter("email");
        String senha = request.getParameter("senha");
     
        try{    
         /*
            Class.forName("com.mysql.jdbc.Driver");  
              
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/cgd","root","");    
            PreparedStatement stmt=con.prepareStatement("select * from usuario where email=? and senha=?");    
                        stmt.setString(1, email);    
                        stmt.setString(2, senha);    
                        ResultSet rs=stmt.executeQuery();
           */             
                         
                        UsuarioDao uD = new UsuarioDao();
                        Boolean cad = uD.verificaLogin(email, senha);
                        
                        if (cad) {
                            System.out.print("Acesso ok,login e senha confere!");   
                            
                            RequestDispatcher disp;
                            this.criaSessao(request, email, senha);
                            disp = request.getRequestDispatcher("index.jsp");
                            disp.forward(request, response);
                        }else{
                            System.out.print("Acesso negado!");
                            RequestDispatcher disp;
                            disp = request.getRequestDispatcher("login.jsp");
                            disp.forward(request, response);
                        }    
            
         }catch(Exception e){    
            System.out.print(e.getMessage());    
         }     
         }    
  
    protected HttpSession criaSessao(HttpServletRequest request, String email, String senha)
    throws ServletException, IOException {
        HttpSession session = request.getSession(true);

        Usuario u = new UsuarioDao().getUsuarioByEmail(email);
		
        session.setAttribute("usuario", u);
        return session;
    } 
}
